<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import s94Mjs from "s94-js";
import {_config, decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = _config('status_alias_map.index_card.type');
    let model_name_config = decode_config(_config('model_map'), {content_key:'label', value_key:'name'});
    let forms_config = {
      title: {label:'板块标题',name:'title',type:'text_one',tips:'',validate:['max:120']},
      type: {label:'板块类型',name:'type',type:'select_one',config:type_map,tips:'选择板块类型',value:''},
      config: {label:'配置',name:'config',type:'json_param',config:[],tips:'',value:'{}'},
      model_name: {label:'数据模型',name:'model_name',type:'select_one',config:model_name_config,tips:'选择板块内容的模型',value:''},
      object_ids: {label:'项目id集',name:'object_ids',type:'list_ids',config:'',tips:'选择板块内容的ID集'},
      href: {label:'跳转链接',name:'href',type:'text_one',tips:''},
      sort: {label:'排序',name:'sort',type:'text_one',config:'number',tips:'',value:50},
      status: {label:'状态',name:'status',type:'radio_switch',config:['禁用','启用'],tips:'',value:1},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
      model_map: {},
    }
  },
  watch: {
    'config.model_name.value'(v){
      let map = ['agent'];
      if (~map.indexOf(v)){
        this.config.object_ids.config = `/${v}/index`;
      }else{
        this.config.object_ids.config = `/model_v2_base/index?model_name=${v}`;
      }
    },
    'config.type.value'(v){
      this.config.model_name.remove = !!~[10].indexOf(v);
      this.config.object_ids.remove = !!~[9,10].indexOf(v);

      let config_map = {
        0:{
          style_type: {label:'轮播图类型',name:'style_type',type:'radio',config:['无边距','有边距'],tips:''},
          speed: {label:'滚动速度',name:'speed',type:'text_one',config:"number",value:"",tips:'设定单次滚动完需要的时间，单位：秒'}
        },
        1:{
          background_color: {label:'背景颜色',name:'background_color',type:'text_one',config:"color",value:"",tips:''},
          color: {label:'字体颜色',name:'color',type:'text_one',config:"color",value:"",tips:''},
        },
        2:{
          background_color: {label:'背景颜色',name:'background_color',type:'text_one',config:"color",value:"",tips:''},
          color: {label:'字体颜色',name:'color',type:'text_one',config:"color",value:"",tips:''},
          limit: {label:'显示数量',name:'limit',type:'text_one',config:"number",value:"",tips:'如果不为空或者0，表示只显示该数量的数据，超出的会在全部里面显示(没有配置跳转链接)'},
        },
        3:{
          background_color: {label:'背景颜色',name:'background_color',type:'text_one',config:"color",value:"",tips:''},
          color: {label:'字体颜色',name:'color',type:'text_one',config:"color",value:"",tips:''},
          limit: {label:'显示数量',name:'limit',type:'text_one',config:"number",value:"",tips:'如果不为空或者0，表示只显示该数量的数据，超出的会在全部里面显示(没有配置跳转链接)'},
        },
        4:{
          background_color: {label:'背景颜色',name:'background_color',type:'text_one',config:"color",value:"",tips:''},
          color: {label:'字体颜色',name:'color',type:'text_one',config:"color",value:"",tips:''},
          limit: {label:'显示数量',name:'limit',type:'text_one',config:"number",value:"",tips:'如果不为空或者0，表示只显示该数量的数据，超出的会在全部里面显示(没有配置跳转链接)'},
        },
        5:{
          background_color: {label:'背景颜色',name:'background_color',type:'text_one',config:"color",value:"",tips:''},
          color: {label:'字体颜色',name:'color',type:'text_one',config:"color",value:"",tips:''},
          limit: {label:'显示数量',name:'limit',type:'text_one',config:"number",value:"",tips:'如果不为空或者0，表示只显示该数量的数据，超出的会在全部里面显示(没有配置跳转链接)'},
        },
        6:{
          head_img: {label:'左侧图标',name:'head_img',type:'file_img',tips:''},
          speed: {label:'滚动速度',name:'speed',type:'text_one',config:"number",value:"",tips:'设定单次滚动完需要的时间，单位：秒'}
        },
        9:{
          head_img: {label:'左侧图标',name:'head_img',type:'file_img',tips:''},
          speed: {label:'滚动速度',name:'speed',type:'text_one',config:"number",value:"",tips:'设定单次滚动完需要的时间，单位：秒'}
        },
        10:{position: {label:'定位',name:'position',type:'radio',config:{top:"置顶", none:"默认"},value:""}},
      }
      this.config.config.config = config_map[v] || '';
      if (this.config.config.config){
        this.config.config.type = 'json_param';
      }else {
        this.config.config.type = 'hidden';
        this.config.config.value = '{}';
      }
    },
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/index_card/edit?id=`+this.id : '/admin/index_card/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index_card/index', '$refs.list_url.list');
          iframe.hide('index_card/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/index_card/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }else if (this.__iframe.info){
      this.$refs['forms'].init(this.__iframe.info);
    }

    //其他初始化操作
  }
}
</script>
